package com.client.core.service;


import com.client.core.ChatClient;
import com.client.core.file.FileHandler;
import com.common.GZipUtils;
import com.common.Msg;
import com.common.MsgUtil;
import com.common.bean.SplitFileInfo;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class FileRec implements Runnable {

    private Msg msg;

    public FileRec(Msg msg) {
        this.msg = msg;
    }

    @Override
    public void run() {
        try {
            String s = new String(GZipUtils.ungzip(msg.getBytes()));
            //接收的文件
            System.out.println("(" + msg.getHe() + ")发来成功接收一个分片文件消息->" + s);
            //对发来的文件进行存储
            String[] split = s.split("\\.");
            //这个文件的上一层文件夹名字
            String s2 = split[0] + "." + split[1];
            String wokerpath = System.getProperty("user.dir");
            String s1 = wokerpath + File.separator + "workerFile" + File.separator + "receive";
            String baseFilePath = s1 + File.separator + s2;
            getFileByBytes(GZipUtils.ungzip(msg.getFile()), baseFilePath, s);
            //给对方发送一个成功收到一个文件，同时请求下一个文件
            ChatClient.channel.writeAndFlush(MsgUtil.OK_FILE(ChatService.me, ChatService.he, s + "处理完成，已经保存"));
            //检查发了多少文件了，发送完毕了，就合并文件
            SplitFileInfo info = SplitFileInfo.info(new File(s1 + File.separator + s2 + File.separator + s2 + "拆分说明.tmp"));
            Long fragmentNum = info.getFragmentNum();
            File file = new File(s1 + File.separator + s2 + File.separator);
            //先检查文件总数够不够
            String[] list = file.list();
            //分片+说明
            if (list.length != fragmentNum + 1) {
                //不够就请求下一个
                ChatClient.channel.writeAndFlush(MsgUtil.REQ_FILE(ChatService.me, ChatService.he, (info.getFileName() + "." + (Integer.parseInt(split[split.length - 2]) + 1) + ".tmp").getBytes()));
            } else {
                //如果文件够了就合并
                System.out.println("文件接收完毕，进行合并");
                FileHandler.merge(s1 + File.separator + s2 + File.separator + s2);
            }


        } catch (Exception e) {

        }


    }


    //将Byte数组转换成文件
    public static void getFileByBytes(byte[] bytes, String filePath, String fileName) {
        BufferedOutputStream bos = null;
        FileOutputStream fos = null;
        File file = null;
        try {
            File dir = new File(filePath);
            if (!dir.exists() && dir.isDirectory()) {// 判断文件目录是否存在
                dir.mkdirs();
            }
            file = new File(filePath + File.separator + fileName);
            fos = new FileOutputStream(file);
            bos = new BufferedOutputStream(fos);
            bos.write(bytes);
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            bos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            fos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
